//------------------------------------------------------------------------------
// Fichiers d'ent�te utilis�s
//------------------------------------------------------------------------------
#include "GlobalThresholdOperator.hpp"

//------------------------------------------------------------------------------
// Définition de constantes
//------------------------------------------------------------------------------

namespace ImageAnalysis
{

	//----------------------------------------------------------------------
	// Constructeur
	//----------------------------------------------------------------------
	GlobalThresholdOperator::GlobalThresholdOperator( Gradiant* gradiant, BinaryImage* image, ThresholdFinder* finder ):
	Operator(),
	thresholdFinder(finder),
	currentGradiant(gradiant),
	currentThresholdImage(image)	
	{	
	
	}

	//----------------------------------------------------------------------
	// Destructeur
	//----------------------------------------------------------------------
	GlobalThresholdOperator::~GlobalThresholdOperator( )
	{
	
	}

	//----------------------------------------------------------------------
	// 
	//----------------------------------------------------------------------
	void GlobalThresholdOperator::Process( )
	{
		// Récupération de la taille de l'image à seuiller ainsi que la valeur du seuil
		int gradiantSize = currentGradiant->GetWidth() * currentGradiant->GetHeight();
		Region region(0, 0, currentGradiant->GetWidth(), currentGradiant->GetHeight() );
		unsigned char* threshold = thresholdFinder->GetOneThreshold( &region, currentGradiant );
		unsigned char value;

		// Seuillage de tous les pixels de l'image
		for( int pixelIndex=0; pixelIndex < gradiantSize; ++pixelIndex )
		{
			value = (currentGradiant->GetGradiantNorm( pixelIndex ) >= threshold[0])?BinaryImage::WHITE_VALUE:BinaryImage::BLACK_VALUE;
			currentThresholdImage->SetPixel(pixelIndex,&value);
		}

		// Suppression de la région
		delete[] threshold;
	}

}


